package com.haiyi.software.app.basic;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.dao.ChatDAO;
import com.haiyi.software.entity.EntityChat;
import com.haiyi.software.entity.EntityMsg;
import com.haiyi.software.entity.HiChatMessage;
import com.haiyi.software.entity.UserFriend;
import com.haiyi.software.tools.MyTools;
import com.haiyi.software.xmpp.event.XMPPEvent;

public abstract class HiChatActivityBasic extends FragmentActivity implements
		XMPPEvent {

	private final static String TAG = "HiChatActivityBasic";

	protected MainApplication app;

	protected ArrayList<HiChatMessage> messages;

	private ChatDAO dao;

	private Message saveMsg;

	protected String str_server = null;

	protected final String str_requestNameUpload = "upload.php";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);

		app = MainApplication.getInstance();

		messages = new ArrayList<HiChatMessage>();

		dao = ChatDAO.getInstence();

		str_server = this.getResources().getString(R.string.URL);

	}

	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		app.addXMPPEvent(this);
	}

	@Override
	protected void onStop() {
		// TODO 自动生成的方法存根
		super.onStop();
		app.removeXMPPEvent(this);
	}

	/**
	 * 把指定人的消息设置为已读
	 * 
	 * @param uid
	 *            指定人的UID
	 */
	protected void changeMsgReadStateToRead(String uid) {

		dao.changeMessageStateToRead(uid);

	}

	/**
	 * 返回我与指定人的对话消息
	 * 
	 * @param uid
	 *            指定人的UID
	 */
	protected void LoadMessageFromDB(String uid) {

		List<EntityChat> chats = dao.getMessageList(uid, "-1");

		int messageState = 1;

		for (int i = (chats.size() - 1); i >= 0; i--) {

			EntityChat ch = chats.get(i);

			switch (Integer.parseInt(ch.getState())) {
			case 1:
				messageState = HiChatMessage.CHATMESSAGE_MINE;
				break;
			case 0:
				messageState = HiChatMessage.CHATMESSAGE_PERSON;
				break;
			default:
				break;
			}

			String str_XML = chats.get(i).getBody();

			EntityMsg entMsg = MyTools.getInstance()
					.getWhichEntityMsgByXMLBody(str_XML);

			entMsg.setMessageFromChat(chats.get(i));

			HiChatMessage msg = new HiChatMessage(entMsg, messageState);

			messages.add(msg);

		}
	}

	/**
	 * 把消息存放到数据库中
	 */
	protected void AddMyMessageToDB(EntityMsg message, UserFriend uf) {

		EntityChat chat = new EntityChat();

		Log.i(TAG, message.getPacket().toXML());

		chat.setBody(message.getPacket().toXML());

		chat.setDownload("0");

		chat.setRead_state("1");// 我自己发的消息，永远是已读

		chat.setHi_id(uf.getUfID());

		chat.setName(uf.getUfName());

		chat.setSiID(message.getPacket().getPacketID());

		chat.setState("1");

		chat.setUhead(uf.getUfPhoto());

		chat.setTimes(message.getDatetime());

		dao.Add(chat);
	}

	@Override
	public void XMPPConnectEvent(XMPPConnection connection) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPAuthenticate(XMPPConnection connection) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPNotAuthenticate(XMPPConnection connection) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPReceiveMessage(Message msg) {
		// TODO 自动生成的方法存根

		// String uid = MyTools.getMessageHiid(msg.getFrom());
		// receiveMessageShow(msg);

		dao.changeMessageStateToReadBySID(msg.getPacketID());

		saveMsg = msg;
		UIMessage.sendEmptyMessage(1);
	}

	protected abstract void receiveMessageShow(Message message);

	Handler UIMessage = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			// TODO 自动生成的方法存根
			super.handleMessage(msg);

			receiveMessageShow(saveMsg);
		}
	};

}
